package com.example.demo.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;

import java.util.ArrayList;
import java.util.List;

public class StudentExcelListener extends AnalysisEventListener<Student> {
    private List<Student> data = new ArrayList<>();
    private StudentService studentService;

    public StudentExcelListener(StudentService studentService) {
        this.studentService = studentService;
    }

    @Override
    public void invoke(Student student, AnalysisContext analysisContext) {
        data.add(student);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println(data);
        if(studentService != null){
            studentService.insert(data);
        }
    }
}
